<?php
/**
 *
 * @package    Action
 * @author     Quentin
 * @since      2015/9/16 15:52
 */


function make_tree($list, $pId = 1)
{
	$tree = array();

	foreach ($list as $key => $item) {
		if ($item ['parentId'] == $pId) {
			$tree[$item['id']] = $item;
			$tree[$item['id']]['kids'] = make_tree($list, $item ['id']);
			unset($list[$key]);
		}
	}

	return $tree;
}

$secondsToCache = 3600;
$ts             = gmdate("D, d M Y H:i:s", time() + $secondsToCache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$secondsToCache");
$conn = new mysqli('192.168.33.13', 'root', 'root', 'test');
$conn->set_charset('utf8');
$result = $conn->query('select * from area');
$list = array();

while(($row = $result->fetch_assoc()) != FALSE) {
	$list[] = $row;
}

$tree = make_tree($list, 0);
file_put_contents(__DIR__.'/public/js/area.json', json_encode($tree));